﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Services;
using System.Web.UI.WebControls;
using nplJobsCommon;
using nplJobsBLL.ProcessJobSeeker;
using nplJobsBLL.Common;
using nplJobsBLL.ProcessJob;
using nplJobsBLL.ProcessEmployeer;
using nplJobsCommon.Common;
using nplJobsCommon.JobSeeker;
using System.Data;
using nplJobsBLL.ProcessSelectJobSeeker;
using System.Threading;
using System.Web.Security;
using nplJobsCommon.Employeer;
using nplJobsDAL.Select;
using System.Text;
using Elmah;

public partial class _Default : BasePage // System.Web.UI.Page
{
    //PagedDataSource pagedDataSource;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {              
                LoadJobCategory();
                LoadGovtJobCategory();
                LoadNewspaperJobCategory();
                
                this.ViewState["pageIndex"] = 0;
                LoadHotJobs();
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
            }
        }
    }

    protected void dlHotJobs_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            List<HotJob> lstHotJob = Session["HotJobsList"] as List<HotJob>;
            if (lstHotJob != null)
            {
                HiddenField drv = e.Item.FindControl("lblEmployeerID") as HiddenField;
                if (drv != null)
                {
                    List<HotJob> hotJobs = lstHotJob.Where(w => w.EmployeerID.Equals(drv.Value)).ToList();

                    if (hotJobs.Count > 2)
                    {
                        System.Web.UI.HtmlControls.HtmlGenericControl expandimage = e.Item.FindControl("expandimage") as System.Web.UI.HtmlControls.HtmlGenericControl;
                        if (expandimage != null)
                        {
                            expandimage.Attributes.CssStyle.Add("display", "block");
                        }
                    }

                    DataList innerHotJobs = e.Item.FindControl("innerHotJobs") as DataList;
                    innerHotJobs.DataSource = hotJobs.Count > 2 ? hotJobs.Take(2).ToList() : hotJobs;
                    innerHotJobs.DataBind();

                    ////////////////Hidden///////////////
                    HiddenField lblEmployeerID1 = e.Item.FindControl("lblEmployeerID1") as HiddenField;
                    if (lblEmployeerID1 != null)
                    {
                        DataList innerHotJobs1 = e.Item.FindControl("innerHotJobs1") as DataList;
                        innerHotJobs1.DataSource = hotJobs;
                        innerHotJobs1.DataBind();
                    }
                }
            }
        }

    }

    /// <summary>
    /// Get job seeker by id
    /// </summary>
    /// <param name="jobSeekerId"></param>
    /// <returns></returns>
    #region GetJobSeeker
    private JobSeeker GetJobSeeker(Guid jobSeekerId)
    {
        JobSeeker jobSeeker = new JobSeeker();
        ProcessSelectJobSeeker selectJobSeeker = new ProcessSelectJobSeeker();

        selectJobSeeker.JobSeekerID = jobSeekerId;
        selectJobSeeker.invoke();

        if (selectJobSeeker.JobSeeker != null)
        {
            jobSeeker = selectJobSeeker.JobSeeker.FirstOrDefault();
        }

        return jobSeeker;
    }
    #endregion

    /// <summary>
    /// Get employeer by id
    /// </summary>
    /// <param name="employeerId"></param>
    /// <returns></returns>
    #region GetEmployeer
    private Employeer GetEmployeer(Guid employeerId)
    {
        Employeer employeer = new Employeer();
        ProcessSelectEmployeer selectEmployeer = new ProcessSelectEmployeer();

        selectEmployeer.employeerID = employeerId;
        selectEmployeer.invoke();

        if (selectEmployeer.Employeer != null)
        {
            employeer = selectEmployeer.Employeer.FirstOrDefault();
        }

        return employeer;
    }
    #endregion

    

    private int GetHeight(int count)
    {
        int height = 255;
        if (count == 3)
        {
            height = 105;
        }
        else if (count == 4)
        {
            height = 115;
        }
        else if (count == 5)
        {
            height = 135;
        }
        else if (count == 6)
        {
            height = 155;
        }
        else if (count == 7)
        {
            height = 175;
        }
        else if (count == 8)
        {
            height = 195;
        }
        else if (count == 9)
        {
            height = 215;
        }
        else if (count == 10)
        {
            height = 235;
        }
        return height;
    }

    private void LoadHotJobs()
    {
        ProcessHotJobs obj = new ProcessHotJobs();
        List<HotJob> lstHotJob = obj.GetHotJobList(-1);

        Session["HotJobsList"] = lstHotJob;

        var commonCompanys = from job in lstHotJob
                             group job by new { job.EmployeerID, job.CompanyName, job.CompanyImage } into g
                             select new { EmployeerID = g.Key.EmployeerID, CompanyName = g.Key.CompanyName, CompanyImage = g.Key.CompanyImage };

        dlHotJobs.DataSource = commonCompanys;
        dlHotJobs.DataBind();
    }
    
    #region LoadControls

    private int LoadOverseasJobsCount()
    {
        SelectOverseasJobs SelectOverseasJobs = new SelectOverseasJobs();
        return SelectOverseasJobs.GetOverseasJobDetailsCount();
    }

    private void LoadJobCategory()
    {
        try
        {
            ProcessJobCategory category = new ProcessJobCategory();
            category.GetCategoryWithJobCount();
            List<JobCategory> lstJobCategory = category.JobCategory;
            lstJobCategory.Add(new JobCategory() { ID = 99, Caterogy = "Overseas Jobs (" + LoadOverseasJobsCount().ToString() + ")" });

            dlList.DataSource = lstJobCategory.OrderBy(od => od.ID).ToList();
            dlList.DataBind();
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }

    private void LoadGovtJobCategory()
    {
        try
        {
            ProcessJobCategory category = new ProcessJobCategory();
            category.GetGovtJobCategoryWithJobCount();
            List<JobCategory> lstJobCategory = category.JobCategory;

            dlGovtJobs.DataSource = lstJobCategory.OrderBy(od => od.ID).ToList();
            dlGovtJobs.DataBind();
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }

    private void LoadNewspaperJobCategory()
    {
        try
        {
            ProcessJobCategory category = new ProcessJobCategory();
            category.GetNewspaperJobCategoryWithJobCount();
            List<JobCategory> lstJobCategory = category.JobCategory;

            dlNewspaperJobs.DataSource = lstJobCategory.OrderBy(od => od.ID).ToList();
            dlNewspaperJobs.DataBind();
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message.ToString());
        }
    }

    //private void getCompanies()
    //{
    //    try
    //    {
    //        //ProcessSelectEmployeer obj = new ProcessSelectEmployeer();
    //        //obj.employeerID = Guid.Empty;


    //        //obj.invoke();

    //        //gvCompany.DataSource = obj.Employeer;
    //        //gvCompany.DataBind();
    //    }
    //    catch (Exception ex)
    //    {

    //    }
    //}

    #endregion
    

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    LoadData(-1, -1, -1, -1, txtWhat.Text.Trim(), txtWhere.Text.Trim(), false);
        //}
        //catch (Exception ex)
        //{

        //}
    }
}